<?php

namespace app\common\model;

use app\common\library\MemberVo;
use think\Model;

/**
 * 模型
 */
class ShopGoods extends Model
{
    // 表名
    protected $name = 'shop_goods';
    protected $deleteTime = false;
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public function goods()
    {
        return $this->belongsTo('goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goodsImage()
    {
        return $this->hasMany(GoodsImage::class, 'goods_id','goods_id');
    }

    public function comment()
    {
        return $this->hasMany('orderComment', 'shop_goods_id');
    }

    public function orderGoods()
    {
        return $this->hasMany(OrderGoods::class, 'goods_id', 'goods_id');
    }

    public function shop_backend()
    {
        return $this->belongsTo('shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
//        return $this->belongsTo(Shop::class, 'shop_id');
    }

    public function shop()
    {
        return $this->belongsTo('shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
//        return $this->belongsTo('shop', 'shop_id');
    }

    // 追加属性
    protected $append = [

    ];

    const STATUS_OK    = 1;
    const STATUS_CLOSE = 0;
    static $statusMap = [
        self::STATUS_OK    => '上架',
        self::STATUS_CLOSE => '下架',
    ];


    const AUDIT_OK      = 1;
    const AUDIT_DEFAULT = 0;
    const AUDIT_REFUSE  = 2;
    static $auditMap = [
        self::AUDIT_OK      => '通过',
        self::AUDIT_DEFAULT => '待审核',
        self::AUDIT_REFUSE  => '拒绝',
    ];

    /**
     * 获取店铺产品信息
     * @param $shop_id
     * @param $goods_id
     * @return array|bool|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getShopGoodsById($shop_goods_id, $shop_id)
    {
        return self::where([
            'shop_id' => $shop_id,
            'status'  => self::STATUS_OK,
        ])->with(['goods'])->find($shop_goods_id);
    }

    /**
     * 根据用户获取相应的价格
     * @param $price
     * @return array
     * @throws \think\exception\DbException
     */
    public static function getPrice($price)
    {
        $member_info = Member::get(MemberVo::instance()->getMemberId());
        $member_discount = Config::getMemberDiscount();//会员折扣
        if ($member_info['discount'] < $member_discount){
            $price_tip = '充值价';
            $new_price = bcmul($price,$member_info['discount'],2); // 折扣后价格
            $discount = $member_info['discount'];
        }else{
            $price_tip = '会员价';
            $new_price = bcmul($price,$member_discount,2); // 折扣后价格
            $discount = $member_discount;
        }
        if ($member_info['is_vip'] == 1){
            $vip_discount = Config::getVipDiscount();//vip折扣

            if ($vip_discount < $discount){
                $price_tip = 'VIP价';
                $new_price = bcmul($price,$vip_discount,2); // 折扣后价格
            }
        }
        if ($new_price < 0.01)
            $new_price = 0.01;
        return ['price'=>$new_price,'price_tip'=>$price_tip];
    }


}
